package dsq.lesson.cookie.history;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dsq.lesson.cookie.entity.Book;
import dsq.lesson.cookie.util.DBUtil;


/**
 * Servlet implementation class ShowAllBooksServlet
 */
@WebServlet("/showAllBooksServlet")
public class ShowAllBooksServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		
		out.write("本网站有以下好书:<br/>");
		
		Map<String, Book> books = DBUtil.findAllBooks();
		
		Set<Entry<String,Book>> entrySet = books.entrySet();
		for (Entry<String, Book> entry : entrySet) {
			out.write("<a href='"+request.getContextPath()+"/showBookDetail?id="+entry.getKey()+"' target='_blank' >"+entry.getValue().getName()+"</a><br/>");
			
		}
		
		out.write("<hr/>您最近浏览的书有<br/>");
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
